# -*- coding: utf-8 -*-
import scrapy
import re
from ..items import ImageItem


class ImgSpider(scrapy.Spider):
    name = 'img'
    allowed_domains = ['http://image.baidu.com/']
    start_urls = [
        'http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1586060182948_R&pv=&ic=&nc=1&z=&hd=&latest=&copyright=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&hs=2&sid=&word=%E7%8C%AB']

    def parse(self, response):
        # data = response.body.decode()
        data = response.text
        imgurls = re.findall('"thumbURL":"(.*?)"', data)
        # print(imgurls)

        #     for img in imgurl:
        #         yield scrapy.Request(url=img,callback=self.get_img,dont_filter=True)
        #
        # def get_img(self, response):
        #     item = BaiduimgItem()
        #     item['content'] = response.body
        #     yield item

        # 使用媒体管道类
        item = ImageItem()
        item['image_urls'] = imgurls
        print('调用媒体管道')
        yield item  # 返回图片url列表
